#include "GraphicsText.h"
#include <QTextBlockFormat>
#include <QTextCursor>
#include <QTextDocument>
#include <QKeyEvent>
#include <QCursor>
#include <QPainter>

GraphicsText::GraphicsText(QGraphicsItem *parent)
	: QGraphicsTextItem(parent)
{
	_alignment = Qt::AlignLeft;
	_isEditable = false;
	init();
}

GraphicsText::GraphicsText( const QString& text, QGraphicsItem *parent /*= 0 */ )
	: QGraphicsTextItem( text, parent )
{
	_alignment = Qt::AlignLeft;
	_isEditable = false;
	init();
}

GraphicsText::~GraphicsText()
{

}

void GraphicsText::setAlignment( Qt::Alignment alignment )
{
	_alignment = alignment;
	QTextBlockFormat format;
	format.setAlignment(alignment);
	QTextCursor cursor = this->textCursor();
	int position = textCursor().position();
	cursor.select(QTextCursor::Document);
	cursor.mergeBlockFormat(format);
	cursor.clearSelection();
	cursor.setPosition(position);
	this->setTextCursor(cursor);
}

void GraphicsText::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
{
	if ( _isEditable )
	{
		// 當使用者 double click，讓使用者可以更改文字
		if (textInteractionFlags() == Qt::NoTextInteraction) {
			setTextInteractionFlags(Qt::TextEditorInteraction);

			this->setFocus();
		} else {
			QGraphicsTextItem::mouseDoubleClickEvent(event);
		}
		// 當文字可以編輯時，更改滑鼠游標圖示，讓使用者認為可以編輯
		this->setCursor(Qt::IBeamCursor);
	}
}

void GraphicsText::init()
{
	updateGeometry();
	connect( this->document(), SIGNAL(contentsChange(int, int, int)),this, SLOT(updateGeometry()));
}

void GraphicsText::updateGeometry()
{
	QPointF topRightPrev = boundingRect().topRight();
	setTextWidth(-1);
	setTextWidth(boundingRect().width());
	setAlignment(_alignment);
	QPointF topRight = boundingRect().topRight();

	if (_alignment & Qt::AlignRight) {
		setPos(pos() + (topRightPrev - topRight));
	} else if ( _alignment & Qt::AlignCenter )
	{
		setPos( pos() - ( topRight - topRightPrev )/2 );
	}
}

int GraphicsText::type() const
{
	return Type;
}

void GraphicsText::focusOutEvent( QFocusEvent *event)
{
	setTextInteractionFlags(Qt::NoTextInteraction);
	this->setCursor(Qt::ArrowCursor);
	QGraphicsTextItem::focusOutEvent(event);
}

void GraphicsText::keyPressEvent( QKeyEvent * event )
{
	// 如果使用者按下 enter 鍵的時候不做換行動作
	if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
		this->clearFocus();
		return;
	}
	QGraphicsTextItem::keyPressEvent(event);
}

void GraphicsText::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
	QColor tempColor = this->defaultTextColor();;

	// 如果文字可以編輯，畫一個白色的底色當作文字輸入框
	if (textInteractionFlags() == Qt::TextEditorInteraction) {
		painter->setBrush(Qt::white);
		painter->drawRect(boundingRect());
		this->setDefaultTextColor(Qt::black);
	} 
	QGraphicsTextItem::paint(painter, option, widget);
	this->setDefaultTextColor(tempColor);
}

void GraphicsText::setEditable( bool editable )
{
	this->_isEditable = editable;
}
